#ifndef __QUEUE_H__
#define __QUEUE_H__

#include <stddef.h>

template <class T>
class FifoRingQueue
{
    public:
        FifoRingQueue(int size=1024):max_len(size){
            this->r_cursor = 0;
            this->w_cursor = 0;
            this->pptr = new T[size];
        }
        ~FifoRingQueue(){
            if (this->pptr != NULL)
            {
                delete [] this->pptr;
                this->pptr = NULL;
            }
        }

        bool push(T d);
        bool pop(T *p);
        int length() const { return this->max_len; }

    private:
        int r_cursor;
        int w_cursor;
        int max_len;
        T* pptr;
};

template <class T>
bool FifoRingQueue<T>::push(T d)
{
    int w_cursor = 0;
    w_cursor = (this->w_cursor + 1) % this->max_len;
    if (w_cursor != this->r_cursor)
    {
        this->pptr[this->w_cursor] = d;
        this->w_cursor = w_cursor;
        return true;
    }

    return false;
}

template <class T>
bool FifoRingQueue<T>::pop(T *p)
{
    if (this->r_cursor != this->w_cursor)
    {
        *p = this->pptr[this->r_cursor];
        this->r_cursor = (this->r_cursor + 1) % this->max_len;
        return true;
    }

    return false;
}

#endif
